package in.ac.dei.edrp.cms.controller.reportgeneration;

import in.ac.dei.edrp.cms.domain.reportgeneration.SemesterInfoForAllStudents;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.view.document.AbstractPdfView;

import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

public class SemesterWisePdfView extends AbstractPdfView {

	@Override
	protected void buildPdfDocument(Map arg0, Document arg1, PdfWriter arg2,
			HttpServletRequest arg3, HttpServletResponse arg4) throws Exception {					
		
	}	
	public void generatePdfForSemesterWiseMarks(Document document,PdfWriter writer,List<SemesterInfoForAllStudents> studentsInfo,String directoryName) throws Exception
	{				
		System.out.println("in semester generation");
		String rollNo = null;
		String entityId = null;
		String programCode = null;
		String name = null;
		String branchId = null;
		String semesterSequence = null;
		
		float[] colsWidth = {1.2f,1.3f,2f,0.8f,0.8f,1f,1f,1f,1f,1f,1f,1f,1f,1f}; // Code 1
		PdfPTable headerTable = new PdfPTable(colsWidth);		
		Font cellFont = new Font(Font.HELVETICA,8);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");		
		Calendar calendar = Calendar.getInstance();
		java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
		String currentDate = sdf.format(date);
							
		
		headerTable.setWidthPercentage(100); // Code 2
		headerTable.setHorizontalAlignment(Element.ALIGN_LEFT);//Code 3		
		headerTable.addCell(new Phrase("FCLBRS",cellFont));
		headerTable.addCell(new Phrase("RollNO.",cellFont));
		headerTable.addCell(new Phrase("Name Of Student",cellFont));
		headerTable.addCell(new Phrase("SA",cellFont));
		headerTable.addCell(new Phrase("Sp",cellFont));
		String fileName;
		System.out.println(currentDate.substring(3,4)+ " "+studentsInfo.get(0).getProgressCardInfo().getSemesterSequence());
		fileName = studentsInfo.get(0).getProgressCardInfo().getProgramCode()+"SS"+studentsInfo.get(0).getProgressCardInfo().getSemesterSequence()+"Y"+
					currentDate.substring(2,4);
		for(int i=1;i<=8;i++)
		{
			headerTable.addCell(new Phrase("WP"+i,cellFont));			
		}
		headerTable.addCell(new Phrase("C.P.",cellFont));
		
		writer = PdfWriter.getInstance(document, new FileOutputStream(directoryName+"\\"+fileName+".pdf"));
		System.out.println("dir name "+directoryName);
		document.open();										
		
		for(SemesterInfoForAllStudents sifas : studentsInfo)
		{
			rollNo = sifas.getProgressCardInfo().getRollNumber();
			entityId = sifas.getProgressCardInfo().getEntityId();
			programCode = sifas.getProgressCardInfo().getProgramCode();
			name = sifas.getProgressCardInfo().getName();
			branchId =sifas.getProgressCardInfo().getBranchId();
			semesterSequence = sifas.getProgressCardInfo().getSemesterSequence();			
		int j=0;	
		
		headerTable.addCell(new Phrase(Integer.parseInt(entityId.substring(4,8))+programCode+branchId+semesterSequence,cellFont));
		headerTable.addCell(new Phrase(rollNo,cellFont));
		headerTable.addCell(new Phrase(name,cellFont));
		headerTable.addCell(new Phrase("F1",cellFont));
		headerTable.addCell(new Phrase("",cellFont));		
		for(int i=1;i<=8;i++)
		{						
			if(i<=sifas.getMarksList().size() && sifas.getMarksList().get(i-1).getStatus().equalsIgnoreCase("PAS"))
			{
				headerTable.addCell(new Phrase(sifas.getMarksList().get(i-1).getTotalAggregate(),cellFont));
			}
			else 
				headerTable.addCell(new Phrase("",cellFont));
		}
		headerTable.addCell(new Phrase("90",cellFont));		
		}
		document.add(headerTable);	
		document.close();
	}

	
}
